﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using ArgusLight.Fractals2D;

namespace Fractals
{
	public static class HistoryManager
	{
		public static event RoutedPropertyChangedEventHandler<bool> CanGoBackChanged;

		public static void Initialize()
		{
			CanGoBack = HistoryList.Count > 1;
		}

		static List<string> HistoryList
		{
			get
			{
				IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
				return new List<string>((string[])settings[SettingNames.History]);
			}
			set
			{
				IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
				settings[SettingNames.History] = value.ToArray();
			}
		}

		private static bool canGoBack = false;
		public static bool CanGoBack
		{
			get { return canGoBack; }
			set
			{
				if (value == canGoBack)
					return;
				bool oldValue = canGoBack;
				canGoBack = value;
				if (CanGoBackChanged != null)
					CanGoBackChanged(null, new RoutedPropertyChangedEventArgs<bool>(oldValue, value));
			}
		}

		public static void Add(Fractal fractal)
		{
			FractalSaveStruct str = new FractalSaveStruct(fractal);

			using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
			{
				List<string> history = HistoryList;

				IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
				if (history.Count > (int)settings[SettingNames.MaxHistoryLength])
				{
					store.DeleteFile(Path.Combine(IsolatedDirectories.History, history[0]));
					history.RemoveAt(0);
				}

				int i = 0;
				string newHistory = i.ToString();
				while (history.Contains(newHistory) == true)
				{
					i++;
					newHistory = i.ToString();
				}

				history.Add(newHistory);
				HistoryList = history;

				if (history.Count > 1)
					CanGoBack = true;

				using (IsolatedStorageFileStream file = store.CreateFile(Path.Combine(IsolatedDirectories.History, newHistory)))
				{
					str.Save(file);
					file.Close();
				}
			}
		}

		public static FractalSaveStruct Pop()
		{
			List<string> history = HistoryList;

			FractalSaveStruct str;
			using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
			{
				string deleteName = Path.Combine(IsolatedDirectories.History, history[history.Count - 1]);
				store.DeleteFile(deleteName);

				history.RemoveAt(history.Count - 1);
				HistoryList = history;

				if (history.Count < 2)
					CanGoBack = false;

				string path = Path.Combine(IsolatedDirectories.History, history[history.Count - 1]);
				using (IsolatedStorageFileStream file = store.OpenFile(path, FileMode.Open))
				{
					str = FractalSaveStruct.FromStream(file);
					file.Close();
				}
			}
			return str;
		}

		public static void ClearHistory()
		{
			using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
			{
				List<string> history = HistoryList;
				foreach (string fileName in history)
				{
					store.DeleteFile(Path.Combine(IsolatedDirectories.History, fileName));
				}
			}

			HistoryList = new List<string>();
			CanGoBack = false;
		}

		public static FractalSaveStruct GetCurrent()
		{
			List<string> history = HistoryList;
			FractalSaveStruct str;
			using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
			{
				string path = Path.Combine(IsolatedDirectories.History, history[history.Count - 1]);
				using (IsolatedStorageFileStream file = store.OpenFile(path, FileMode.Open))
				{
					str = FractalSaveStruct.FromStream(file);
					file.Close();
				}
			}
			return str;
		}

		public static int GetCount()
		{
			return HistoryList.Count;
		}
	}
}
